package edu.pku.dao;

import java.sql.SQLException;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;

import edu.pku.data.User;
import edu.pku.util.MySqlMapClient;

public class UserDao {
	
	public void addUserNoCompany(User user) throws SQLException
	{
		SqlMapClient sqlMap = MySqlMapClient.getSqlMapInstance();
		sqlMap.insert("insert_user_without_company",user);
	}
	
	public void addUserCompany(User user) throws SQLException
	{
		SqlMapClient sqlMap = MySqlMapClient.getSqlMapInstance();
		sqlMap.insert("insert_user_with_company",user);
	}
	
	public void updateUser(User user) throws SQLException
	{
		SqlMapClient sqlMap = MySqlMapClient.getSqlMapInstance();
		sqlMap.update("update_user",user);
	}
	
	public boolean isNameExist(String name) throws SQLException
	{
		SqlMapClient sqlMap = MySqlMapClient.getSqlMapInstance();
		List<String> list = sqlMap.queryForList("get_user_name");
		if(list.contains(name))
			return true;
		return false;
	}
	
	public User getUserById(int id) throws SQLException
	{
		SqlMapClient sqlMap = MySqlMapClient.getSqlMapInstance();
		User user = (User)sqlMap.queryForObject("get_user_by_id",id);
		return user;
	}
	
	public User getUserByName(String name) throws SQLException
	{
		SqlMapClient sqlMap = MySqlMapClient.getSqlMapInstance();
		User user = (User)sqlMap.queryForObject("get_user_by_name",name);
		return user;
	}
	
	public List<User> getUsersByCompany(int id) throws SQLException
	{
		SqlMapClient sqlMap = MySqlMapClient.getSqlMapInstance();
		List<User> list =  sqlMap.queryForList("get_users",id);
		return list;
	}
}
